Repository navigation
Conversation
Co-authored-by: Anthony Kim <anthonykim@microsoft.com>
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
I have made the requested changes; please review again |
|
Thanks for making the requested changes! @brettcannon: please review the changes made to this pull request. |
Preserve both ANY and MagicMock when merging the interactive console tests, keeping upstream prompt coverage and statement-submitted hook coverage intact. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 51fc76fa-b241-49aa-830c-840d2d812b54
|
@pablogsal @ambv feel free to review, but if you're too busy I will just plan to merge in about a week. |
| patch.object(simple_interact, "append_history_file"), | ||
| patch("_pyrepl.readline._setup"), | ||
| contextlib.redirect_stdout(output), | ||
| contextlib.redirect_stderr(output), |
There was a problem hiding this comment.
I wouldn't rely on mocks of internal attributes.
I'd test this with a PTY instead, maybe in test_pyrepl.TestMain?
That ensures the things that need to be displayed to the user are actually displayed to the user.
Example:
cpython/Lib/test/test_pyrepl/test_pyrepl.py
Lines 2482 to 2492 in d948c28
(This applies to all tests in this PR, I'd try to fit all of them into a few tests in test_pyrepl.TestMain)
There was a problem hiding this comment.
A PTY test makes sense here. The unit tests still exercise real statement execution, so I see no need to replace all of them.
One detail: the example needs flush=True, or the execution marker can stay in the buffer until the statement finishes. Our PTY helper uses -u, so it will miss that case.
There was a problem hiding this comment.
Added e28071a0a0a: Add PTY test.
The new TestMain test installs the hook through actual REPL input and checks that labelled hook errors reach the terminal while subsequent statements still execute. I retained the focused unit tests, following Pablo's suggestion. The inline escape-sequence example has also been removed in b7e9986fc46: Simplify comment to remove example; this PTY test does not claim to validate buffering behavior.
Validation: .\PCbuild\amd64\python.exe -m test -v test_pyrepl.test_interact test_pyrepl.test_pyrepl -m '*TestStatementSubmittedHook*' -m '*statement_submitted_hook*' passed on Windows: 9 hook unit tests passed, and 2 PTY test variants were skipped. Unix execution of the new PTY test is still pending.
There was a problem hiding this comment.
This test is still relying on mocking internal attributes, is it possible to not do it that way?
| try: | ||
| statement_submitted_hook(statement) | ||
| except Exception: | ||
| console.showtraceback() |
There was a problem hiding this comment.
It's now easy to misread a traceback from the hook's exception as an exception that comes from running the user code:
>>> from functools import singledispatch
Traceback (most recent call last):
File "<python-input-7>", line 2, in hook
1/0
~^~
ZeroDivisionError: division by zero
>>>(I've set the hook in the session, but even if there's internal VSCode paths included in the traceback that are telling this is some hook hackery, I still think this could be improved.)
Additionally, that traceback is displayed before the user code exception traceback.
I'd prefer:
- Reporting the full traceback to
reader.debug()(a REPL hacker can then uncommentdebug()code for verbose error reporting) - Reporting that an "internal" error happened to
reader.error()error overlay (something like "exception occurred while running an internal prompt hook")
There was a problem hiding this comment.
I agree the traceback needs a clear hook label, but I prefer to keep it visible. reader.debug() currently does nothing, so that loses useful details.
There was a problem hiding this comment.
There is a behavior issue with console.showtraceback() here. It replaces sys.last_exc and the other sys.last_* attributes. If the hook fails on each submission, pdb.pm() sees the hook error instead of the previous user exception. I think hook error reports need to preserve that state. This also needs a regression test.
There was a problem hiding this comment.
Addressed in 866576906ce: Show traceback as a hook failure without modifying sys.last_*.
The full traceback stays visible with an Error in statement_submitted_hook: heading. It uses the existing console renderer directly, preserving coloring without overwriting sys.last_*. Added regression tests for coloring and for preserving both existing and initially absent last-exception attributes.
|
Hello, when you have the chance, can I please get another review here? @pablogsal @ambv @johnslavik |
|
@edvilme Could you please use your own words when replying? Thanks |
| console.write("Error in statement_submitted_hook:\n") | ||
| console._excepthook(type(exc), exc, exc.__traceback__) |
There was a problem hiding this comment.
_excepthook writes to console directly, but it would make sense to me if we reused sys.excepthook like _showtraceback, just without writing to sys attributes. Maybe add a param to showtraceback and _showtraceback for REPL-specific errors to just avoid writing to sys attributes but go through the traceback display pipeline inside _showtraceback? Or simply move the pipeline to _excepthook, since no call sites (other than _showtraceback) use _excepthook directly
WDYT @pablogsal?
Supersedes #144724
Adds a
statement_submitted_hookhook to_pyrepl.simple_interactthat gets triggered after the user has submitted a statement (not necessarily a line) but before it is executed.Rationale
This should be configurable by external tools such as IDEs who may want to perform an action before a statement is executed, such as displaying custom escape sequences for terminal integration, handling custom logging logic, performing analysis, etc.
This hook should allow said tools to perform an action.
Example (Timer)
Example (VSCode)
VS Code uses custom escape sequences to indicate when a command execution has started and finished. Here, the hook could be used to print an invisible escape sequence to the terminal. (Note that the example uses
### EXEC ###to represent it visually, but escape sequences are invisible)With error reporting
AI Usage
AI was used to help with merge conflicts